package cn.endeavour.quartz.service.impl;


import cn.endeavour.quartz.domain.QuartzJobInfo;
import cn.endeavour.quartz.job.MainJob;
import cn.endeavour.quartz.service.IQuartzService;
import cn.endeavour.quartz.utils.QuartzUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

@Service
public class QuartzServiceImpl implements IQuartzService {
    @Autowired(required = false)
    private SchedulerFactoryBean schedulerFactoryBean;

    /**
     * 添加定时任务
     */
    @Override
    public void addJob(QuartzJobInfo quartzJobInfo) {
        QuartzUtils.addJob(schedulerFactoryBean.getScheduler(), quartzJobInfo.getJobName(), MainJob.class, quartzJobInfo, quartzJobInfo.getCron());
    }

    /**
     * 移除定时任务
     */
    @Override
    public void removeJob(String jobName) {
        QuartzUtils.removeJob(schedulerFactoryBean.getScheduler(), jobName);
    }
}
